package com.aooci.ordercat.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;

import com.aooci.ordercat.dao.ItemDAO;
import com.aooci.ordercat.domain.Item;
import com.aooci.ordercat.vo.ItemVo;

/**
 * A data access object (DAO) providing persistence and search support for
 * Account entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.bitongline.lgs.dao.Account
 */
public class ItemDAOImpl extends AbstractBaseDAOImpl<Item, Integer> implements ItemDAO {

	public ItemDAOImpl() {
		super(Item.class);
	}

	private static final long serialVersionUID = -4236573107157049581L;
	private static final Logger log = Logger.getLogger(ItemDAOImpl.class);
	@Override
	public List<ItemVo> selectByItemCategoryId(final int itemCategoryId) {
	    return getHibernateTemplate().execute(new HibernateCallback<List<ItemVo>>() {  

	        @SuppressWarnings("unchecked")
			@Override  
	        public List<ItemVo> doInHibernate(Session session) {  
	        	Query query = session.getNamedQuery("ITEM.selectByItemCategoryId");
	        	query.setParameter("itemCategoryId", itemCategoryId);
	            return (List<ItemVo>)query.list();  
	        }  
	    });
	}
	@Override
	public Item findByLabel(final String label) {
	    return getHibernateTemplate().execute(new HibernateCallback<Item>(){
	        @Override  
	        public Item doInHibernate(Session session) {  
	        	Query query = session.getNamedQuery("ITEM.findByLabel");
	        	query.setParameter("label", label);
	        	@SuppressWarnings("unchecked")
				List<Item> list = query.list();
	            return list.isEmpty()? null : list.get(0);  
	        }  
	    });
	}
	@Override
	public List<ItemVo> selectAll() {
	    return getHibernateTemplate().execute(new HibernateCallback<List<ItemVo>>() {  
	        @SuppressWarnings("unchecked")
			@Override  
	        public List<ItemVo> doInHibernate(Session session) {  
	        	Query query = session.getNamedQuery("ITEM.selectAll");
	            return (List<ItemVo>)query.list();  
	        }  
	    });
	}



}